/***************************************************************************
 *
 * Copyright (c) 2021 ShanghaiTech. All Rights Reserved
 * Author: aqiang <heqh1@shanghaitech.edu.cn> <2021-09-27>
 * @brief live
 * 
 **************************************************************************/
#pragma once
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <vector>
#include <termios.h>
#include <fcntl.h> 
#include <mutex>
#include <memory>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/pixdesc.h"
#include "libavutil/hwcontext.h"
#include "libavutil/opt.h"
#include "libavutil/avassert.h"
#include "libavutil/imgutils.h"
#include "libavdevice/avdevice.h"
#include "libavutil/channel_layout.h"
#include "libavutil/mathematics.h"
#include "libavutil/timestamp.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/time.h"
}
using namespace std;

namespace fv {

struct remux_param {
    AVFormatContext *ifmt_ctx = NULL;
    int stream_index = 0;
    int stream_mapping_size = 0;	
	vector<int> stream_mapping;	
	int audio_stream = -1;
	int video_stream = -1;
};

class Demux {
public:
	Demux();

	/****************
	 * 
	 * @brief live demux to solve camera dropped
	 * 
	 ****************/

	/****************
	 * 
	 * @brief player demux
	 * 
	 ****************/
	int demux_packet(int *video_count, vector<AVPacket*> &list_video);

	/****************
	 * 
	 * @brief player common
	 * 
	 ****************/
	int demux_init(string InputUrl);
	int get_param(struct remux_param &param);
	int set_id(const int id);
	bool is_end(int64_t pts);
	~Demux();

private:
    AVFormatContext *ifmt_ctx = NULL;
    int stream_index = 0;
    int stream_mapping_size = 0;
	vector<int> stream_mapping;
	int video_stream = -1;
	int audio_stream = -1;
	int id;
};

}